Retrieving Part Info
To retrieve data from the database, you will set up Flash Remoting as the communication channel between Macromedia Flash and ColdFusion. Here is an overview of what you will do in exercise 1.
Include the NetServices.as file to provide Flash Remoting functionality. The file to include in the ActionScript is called NetServices.as. This file contains the code to create the NetServices object and the methods for implementing Flash Remoting. The syntax is:
#include "NetServices.as"
Create a connection to the server with
Flash Remoting server. To create the connection to the Flash Remoting server, you will use a NetServices
method. You will create a connection variable that you will use later.
Using the createGatewayConnection() method establishes the bidirectional connection between Macromedia Flash and
the Flash Remoting service. The general syntax is:
myConnection_conn=NetServices.createGatewayConnection(URL);
If you are using the built-in ColdFusion MX web server,
the URL variable is: http://127.0.0.1:8500/flashservices/gateway.
Point to the ColdFusion component that
contains the methods you wan to use. By using the connection variable you just created to point to a specific CFC,
you create a web service, using the getService() method of the connection variable. The general syntax is:
myService=myConnection_conn.getService(serviceName, defaultResponder);
The serviceName is the path and name of the CFC. In this case, you will not use a default responder, since you will provide a responder object when you invoke the method.
Invoke a specific method. As just mentioned, you will not use a default responder, so you must create an object that will act as the responder and then use it when you invoke the method. The general syntax is:
myService.methodName(responderObject);
Handle the results the specific method
returns. You will send the results back to the responder object specified in the method
invocation. You automatically call the onResult() method when the results return from Flash Remoting. The general format of the
method:
responderObject.onResult=function(results){
ActionScript statements…
}
The results variable stores the returned results.
Follow the steps in exercise 1 for detailed step-by-step instructions.
Exercise 1
Here are the detailed instructions for the overview above.
- Examine the ColdFusion component.
- Use Dreamweaver MX, or any text editor, to open the file bikeparts.cfc located at C:\CFusionMX\wwwroot\devnet.
-
Later in this exercise, you will need to call the
getPartInfo()function in bikeparts.cfc, or the bikeparts component. Examine the bikeparts component to find thecfquerytag that queries the database and receives data about bicycle parts. The query is:<cfquery name="qPartInfo" datasource="bikeparts"> SELECT Parts.partID, Parts.name, Parts.description, Parts.price, PartType.partType FROM Parts, PartType WHERE Parts.partTypeID=PartType.partTypeID ORDER BY Parts.name </cfquery>
-
Set up Flash Remoting.
- Launch Macromedia Flash and open the devnet_begin1.fla file located at C:\CFusionMX\wwwroot\devnet\.
- Save the file as devnet.fla. You will use this same file in all of the tutorial exercises.
- Be sure you have frame 1 of the Actions layer highlighted and open the Actions panel.
-
At the top of the template, in the included ActionScript files section, include the NetServices.as file, using the following syntax:
#include "NetServices.as"
Note: Remember, you can simply select the NetServices.as file from the ActionScript toolbox under the Remoting section, as shown here.
Figure 4. Selecting the ActionScript toolbox.
-
At the bottom of the template, in the run at once commands section, create a variable local to the page, using the var statement, named net_conn. Set the connection to http://127.0.0.1:8500/flashservices/gateway. Use the following syntax:
var net_conn = NetServices.createGatewayConnection("http://127.0.0.1:8500/flashservices/gateway"); -
Just under the previous code you entered, create a service named partService that points to the bikeparts CFC that is located under the webroot in the devnet folder. Use the
getService()method. Use the following syntax:var partService = net_conn.getService("devnet.bikeparts")
-
Create the responder object and invoke a remote method
-
Before you can invoke the method, you must create a responder object. In the object creation section, create an object named partResponder using the following syntax:
var partResponder=new Object();
-
Back in the run at once commands section, after the code that creates the service, invoke the
getPartInfo()method of the CFC using partResponder as the responder object, as shown here:partService.getPartInfo(partResponder);
-
-
Build a method to handle the results
-
In the object methods section, create the skeleton of the method that the app automatically calls when data returns. It will be a method of the partResponder object and will use the
onResult()method. The argument of the function is parts_rs. The skeleton should have the following code:partResponder.onResult=function(parts_rs){ } -
Be sure you receive data; in the
onResult()method, use the trace statement to define that you are in the method, as shown here:trace("in the method"); - Save the document.
- Test the movie. Press Control + Enter. You will see an Output window with the in the method text displayed.
- Close the Output window and the movie test window.
-
-
Use the NetConnection Debugger to see what kind of data Flash Remoting and the database pass.
-
Use the NetConnection debugger. In the included ActionScript files section, just below the first include, include the file needed for the NetConnection Debugger, NetDebug.as.
- Select Window > NetConnection Debugger to display the panel.
- Test the movie again.
- In the Events section of the NetConnection Debugger, click Result.
-
See the records returned by the query discussed earlier. One of the records is:
Note: Flash Remoting inserts the __ID__. The other five fields are from the database.
-
Submit feedback on our tutorials, articles, and sample applications.